package com.pro01.apirednote.mapper;

import com.pro01.apirednote.pojo.Content;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ContentMapper {

    // 发布内容
    @Insert("INSERT INTO contents (user_id, title, type, description, created_at, updated_at) " +
            "VALUES (#{userId}, #{title}, #{type}, #{description}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertContent(Content content);

    // 修改内容
    @Select("SELECT * FROM contents WHERE id = #{id}")
    Content selectById(Long id);

    @Update("UPDATE contents SET title = #{title}, description = #{description}, updated_at = NOW() WHERE id = #{id}")
    int updateContent(Content content);

    // 删除内容
    @Update("UPDATE contents SET is_deleted = true WHERE id = #{id}")
    int markContentDeleted(Long id);

    // 获取指定内容点赞数量
    @Select("SELECT like_count FROM contents WHERE id = #{contentId} AND is_deleted = false")
    Integer getLikeCountByContentId(@Param("contentId") Long contentId);

    // 更新浏览量
    @Update("UPDATE contents SET view_count = view_count + 1 WHERE id = #{id}")
    void incrementViewCount(@Param("id") Long id);

    // 评论数 +1
    @Update("UPDATE contents SET comment_count = comment_count + 1 WHERE id = #{contentId}")
    void incrementCommentCount(Long contentId);

    // 评论数 -1
    @Update("""
    UPDATE contents
    SET comment_count = CASE
        WHEN comment_count > 0 THEN comment_count - 1
        ELSE 0
    END
    WHERE id = #{contentId}
""")
    void decrementCommentCount(Long contentId);

    // 查看我发布的内容
    @Select("SELECT * FROM contents WHERE user_id = #{userId} AND is_deleted = FALSE ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> selectMyContent(@Param("userId") Long userId,
                                  @Param("limit") int limit,
                                  @Param("offset") int offset);


    @Select("SELECT COUNT(*) FROM contents WHERE user_id = #{userId} AND is_deleted = FALSE")
    int countMyContent(@Param("userId") Long userId);

    // 根据标题关键字搜索内容
    @Select("SELECT * FROM contents WHERE title LIKE CONCAT('%', #{keyword}, '%') AND is_deleted = FALSE " +
            "ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> searchByTitle(@Param("keyword") String keyword,
                               @Param("limit") int limit,
                               @Param("offset") int offset);

    // 计算搜索结果总数
    @Select("SELECT COUNT(*) FROM contents WHERE title LIKE CONCAT('%', #{keyword}, '%') AND is_deleted = FALSE")
    int countSearchResults(@Param("keyword") String keyword);
    
    // 获取全部内容列表
    @Select("SELECT * FROM contents WHERE is_deleted = FALSE ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> selectAllContent(@Param("limit") int limit, @Param("offset") int offset);

    @Select("SELECT * FROM contents WHERE is_deleted = FALSE ORDER BY created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> selectContentsPaged(@Param("limit") int limit, @Param("offset") int offset);

    // 计算全部内容总数
    @Select("SELECT COUNT(*) FROM contents WHERE is_deleted = FALSE")
    int countAllContent();

    // 管理员删除内容
    @Update("UPDATE contents SET is_deleted = true WHERE id = #{contentId}")
    int adminDeleteContentById(@Param("contentId") Long contentId);

    // 获取推荐内容（按点赞数排序）
    @Select("SELECT * FROM contents WHERE is_deleted = FALSE ORDER BY like_count DESC, created_at DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> selectRecommendedContent(@Param("limit") int limit, @Param("offset") int offset);

    @Select("SELECT * FROM contents WHERE is_deleted = FALSE ORDER BY like_count DESC LIMIT #{limit} OFFSET #{offset}")
    List<Content> selectTopContentsByLikes(@Param("limit") int limit, @Param("offset") int offset);

    @Select("""
    SELECT * FROM contents 
    WHERE is_deleted = FALSE AND id NOT IN (
        SELECT content_id FROM content_views WHERE user_id = #{userId}
    )
    ORDER BY like_count DESC
    LIMIT #{limit} OFFSET #{offset}
""")
    List<Content> selectUnviewedTopContents(@Param("userId") Long userId,
                                            @Param("limit") int limit,
                                            @Param("offset") int offset);

    @Select("SELECT COUNT(*) FROM contents WHERE is_deleted = FALSE")
    int countAllContents();

    @Select("""
    SELECT COUNT(*) FROM contents 
    WHERE is_deleted = FALSE AND id NOT IN (
        SELECT content_id FROM content_views WHERE user_id = #{userId}
    )
""")
    int countUnviewedContents(@Param("userId") Long userId);

    // 计算推荐内容总数
    @Select("SELECT COUNT(*) FROM contents WHERE is_deleted = FALSE")
    int countRecommendedContent();

    // 获取所有内容
    @Select("SELECT * FROM contents")
    List<Content> getAllContents();

    // 获取所有未删除的内容
    @Select("SELECT * FROM contents WHERE is_deleted = FALSE ORDER BY id")
    List<Content> selectAllNotDeleted();

}
